let customer = {}
let socket   = new WebSocket(window.socket_url + '?user_id=' + window.customer_id + '&user_type=2')

Message.sendMsg(socket)

socket.onmessage = function (result) {
    let data = JSON.parse(result.data)
    // 消息发送接收成功
    if (data.code === 200) {
        let msg_type = data.msg_type
        let msg_data = data.data
        switch (msg_type) {
            case 'online':
                // 上线返回基本数据信息
                customer = msg_data['customer']
                break
            case 'userOnline':
                // 用户上线
                Message.userOnline(msg_data, msg_data.room_id)
                break
            case 'message':
                // 用户发送消息
                Message.receiveMsg(msg_data.message)
                break
            case 'userLogoff':
                // 用户下线
                Message.userLogoff(msg_data.room_id)
                break
            default:
                Message.sweetTimer('未知的消息类型~')
        }
    } else {
        Message.sweetTimer(data.data.message)
    }
}

// 关闭连接
socket.onclose = function () {
    Message.sweetTimer('连接已关闭')
}

// 窗口关闭是 主动关闭服务
window.onbeforeunload = function () {
    socket.close()
}
